package com.ruoyi.web.controller.hazard;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hazard.domain.HazardRiskEquipment;
import com.ruoyi.hazard.service.IHazardRiskEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设施风险Controller
 * 
 * @author ruoyi
 * @date 2025-10-25
 */
@RestController
@RequestMapping("/hazard/equipment")
public class HazardRiskEquipmentController extends BaseController
{
    @Autowired
    private IHazardRiskEquipmentService hazardRiskEquipmentService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询设施风险列表
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(HazardRiskEquipment hazardRiskEquipment)
    {
        startPage();
        List<HazardRiskEquipment> list = hazardRiskEquipmentService.selectHazardRiskEquipmentList(hazardRiskEquipment);
        if(!list.isEmpty()) {
            Map<Long, String> deptNameMap = sysDeptService.buildDeptNameMap(list.stream().map(HazardRiskEquipment::getDeptId).collect(Collectors.toList()));
            list.forEach(t -> { t.setDeptName(deptNameMap.get(t.getDeptId())); });
        }
        return getDataTable(list);
    }

    /**
     * 导出设施风险列表
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:export')")
    @Log(title = "设施风险", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HazardRiskEquipment hazardRiskEquipment)
    {
        List<HazardRiskEquipment> list = hazardRiskEquipmentService.selectHazardRiskEquipmentList(hazardRiskEquipment);
        if(!list.isEmpty()) {
            Map<Long, String> deptNameMap = sysDeptService.buildDeptNameMap(list.stream().map(HazardRiskEquipment::getDeptId).collect(Collectors.toList()));
            list.forEach(t -> { t.setDeptName(deptNameMap.get(t.getDeptId())); t.setResult(StringUtils.join(t.getConsequence(), ",")); });
        }
        ExcelUtil<HazardRiskEquipment> util = new ExcelUtil<HazardRiskEquipment>(HazardRiskEquipment.class);
        util.exportExcel(response, list, "设施风险数据");
    }

    /**
     * 获取设施风险详细信息
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hazardRiskEquipmentService.selectHazardRiskEquipmentById(id));
    }

    /**
     * 新增设施风险
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:add')")
    @Log(title = "设施风险", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HazardRiskEquipment hazardRiskEquipment)
    {
        hazardRiskEquipment.setCreateBy(SecurityUtils.getUsername());
        return toAjax(hazardRiskEquipmentService.insertHazardRiskEquipment(hazardRiskEquipment));
    }

    /**
     * 修改设施风险
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:edit')")
    @Log(title = "设施风险", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HazardRiskEquipment hazardRiskEquipment)
    {
        hazardRiskEquipment.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(hazardRiskEquipmentService.updateHazardRiskEquipment(hazardRiskEquipment));
    }

    /**
     * 删除设施风险
     */
    @PreAuthorize("@ss.hasPermi('hazard:equipment:remove')")
    @Log(title = "设施风险", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hazardRiskEquipmentService.deleteHazardRiskEquipmentByIds(ids));
    }


    @ApiOperation("下载设施风险文件模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<HazardRiskEquipment> util = new ExcelUtil<HazardRiskEquipment>(HazardRiskEquipment.class);
        util.importTemplateExcel(response, "设施风险数据");
    }

    @PreAuthorize("@ss.hasPermi('hazard:work:add')")
    @ApiOperation("导入设施风险")
    @Log(title = "设施风险", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<HazardRiskEquipment> util = new ExcelUtil<HazardRiskEquipment>(HazardRiskEquipment.class);
        List<HazardRiskEquipment> hazardRiskWorkVos = util.importExcel(file.getInputStream());
        hazardRiskWorkVos.stream().forEach(t -> t.setConsequence(StringUtils.isBlank(t.getResult()) ? Lists.newArrayList() : Arrays.asList(t.getResult().split(","))));
        String message = hazardRiskEquipmentService.importData(hazardRiskWorkVos, updateSupport);
        return success(message);
    }
}
